package com.team2.outpatient_sys.dao;

import com.team2.outpatient_sys.pojo.Doctor;
import com.team2.outpatient_sys.pojo.DoctorAndDepartment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DoctorDao {
    /**
     * 添加新的医护人员信息
     * @param doctor 添加的医护人员信息的封装对象
     * @return 添加新的医护人员信息影响的表数据行数
     */
    @Insert("INSERT INTO `doctor` VALUES(null, #{name}, #{username}, #{password}, #{gender}, #{birth}, #{phone}, #{deptId}, #{hiredate}, #{isDirector})")
    int insertNew(Doctor doctor);

    /**
     * 修改表中的某一条医护人员信息
     * 需要提供一个完整的医护人员信息封装对象，根据其中的id属性值修改对应的医护人员信息
     * @param doctor 修改的医护人员信息的封装对象
     * @return 修改医护人员信息所影响的表数据行数
     */
    @Update("UPDATE `doctor` SET `name`=#{name}, `username`=#{username}, `password`=#{password}, `gender`=#{gender}, `birth`=#{birth}, `phone`=#{phone}, `dept_id`=#{deptId}, `hiredate`=#{hiredate}, `is_director`=#{isDirector} WHERE `id`=#{id}")
    int updateWholeById(Doctor doctor);

    /**
     * 根据id主键删除每一行医护人员信息
     * @param id 需要删除的医护人员信息的id字段值
     * @return 删除医护人员信息所影响的表数据行数
     */
    @Delete("DELETE FROM `doctor` WHERE `id`=#{id}")
    int deleteById(Integer id);

    /**
     * 查询所有医护人员信息
     * @return 查询到的医护人员信息封装对象Doctor的集合
     */
    @Select("SELECT * FROM `doctor`")
    List<Doctor> queryAll();

    /**
     * 根据id主键查询某一医护人员信息
     * @param id 查询医护人员信息需要的id字段值
     * @return 查询到的医护人员信息封装对象Doctor
     */
    @Select("SELECT * FROM `doctor` WHERE `id`=#{id}")
    Doctor queryById(Integer id);

    /**
     * 根据医护人员的主键id，查询该医护人员所处的科室的id
     * @param doctorId 需要查询科室的医护人员的id字段值
     * @return 该医护人员所处的科室的id字段值
     */
    @Select("SELECT `dept_id` FROM `doctor` WHERE `id`=#{doctorId}")
    Integer queryDeptIdByDoctorId(Integer doctorId);

    /**
     * 根据医护人员的主键id，查询该医护人员信息和所处的科室信息
     * @param doctorId 需要查询信息的医护人员的id字段值
     * @return 该医护人员信息及其科室信息的封装对象DoctorAndDepartment
     */
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "birth", column = "birth"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "deptId", column = "dept_id"),
            @Result(property = "hiredate", column = "hiredate"),
            @Result(property = "isDirector", column = "is_director"),
            @Result(property = "department", column = "dept_id",
                    one = @One(select = "com.team2.outpatient_sys.dao.DepartmentDao.queryById"))
    })
    @Select("SELECT * FROM `doctor` WHERE `id`=#{id}")
    DoctorAndDepartment queryDoctorAndDeptById(Integer doctorId);

    /**
     * 根据deptId，查询所有对应的医护人员的信息
     * @param deptId 需要查询的所处科室的id主键
     * @return 查询到的某一科室下的所有的医护人员的集合
     */
    @Select("SELECT * FROM `doctor` WHERE `dept_id`=#{deptId}")
    List<Doctor> queryByDeptId(Integer deptId);

    /**
     * 根据username查询医护人员的信息
     * @param username 需要查询的username用户名字段值
     * @return 查询到的医护人员信息封装对象Doctor
     */
    @Select("SELECT * FROM `doctor` WHERE `username`=#{username}")
    Doctor queryByUsername(String username);

    /**
     * 查询所有医护人员信息和所处的科室信息
     * @return 查询的结果的封装对象的集合
     */
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "birth", column = "birth"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "deptId", column = "dept_id"),
            @Result(property = "hiredate", column = "hiredate"),
            @Result(property = "isDirector", column = "is_director"),
            @Result(property = "department", column = "dept_id",
                    one = @One(select = "com.team2.outpatient_sys.dao.DepartmentDao.queryById"))
    })
    @Select("SELECT * FROM `doctor`")
    List<DoctorAndDepartment> queryAllAndDept();

    //根据id和密码查询
    @Select("select * from doctor where id=#{id} and password=#{password}")
    Doctor findByIdAndPass(@Param("id") Integer id, @Param("password") String password);

    //修改密码
    @Update("update doctor set password=#{password} where id=#{id}")
    Integer updateNewPass(@Param("password") String password, @Param("id") Integer id);

    //根据用户名查询，判断用户名是否可用
    @Select("select * from doctor where username=#{username}")
    Doctor findByUsername(String username);
}
